package com.hqyj.ctxp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.ctxp.mapper.ProductMapper;
import com.hqyj.ctxp.pojo.Product;
import com.hqyj.ctxp.service.ProductService;
import com.hqyj.ctxp.utils.PageBean;
import com.hqyj.ctxp.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  Product 对应的 Service 实现类
 *      ServiceImpl<M extends BaseMapper<T>, T>
 *          ServiceImpl 类继承了 BaseMapper 接口
 *          ProductServiceImpl 继承了 ServiceImpl 就能直接使用 BaseMapper 不需要注入
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    // 添加商品
    @Override
    public ResultInfo addProduct(Product product) {
        int i = productMapper.insert(product);
        if (i > 0) {
            return new ResultInfo(200, "add success", i);
        }
        return new ResultInfo(200, "add fail", i);
    }

    // 根据商品名删除商品
    public ResultInfo deleteProductByName(String productName) {
        int i = productMapper.delete(new QueryWrapper<Product>().eq("product_name", productName));
        if (i > 0) {
            return new ResultInfo(200, "delete success", i);
        }
        return new ResultInfo(500, "delete fail", i);
    }

    // 根据商品种类，删除对应种类商品

    // 查询商品，分页查询、模糊查询
    @Override
    public ResultInfo selectProductPage(PageBean pageBean) {
        Page<Product> page = new Page(pageBean.getCurrentPage(), pageBean.getPageSize());
//        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        // 模糊查询条件
//        if (productPage.getKeyword() != null) {
//            queryWrapper.and(c -> c.like(Product::getProductName, productPage.getKeyword())
//                    .or().like(Product::getCategory, productPage.getKeyword()));
//        }

        // 排序条件
//        if (productPage.getSort() != null && productPage.getDirection() != null) {
//            // sort --> "", order --> desc, asc
//            queryWrapper.last("order by".concat(productPage.getDirection()).concat(" ").concat(productPage.getSort()));
//        }
        // Page<Product> productPageResult = productMapper.selectPage(page,null);
        Page<Product> productPageResult = productMapper.selectProductPage(page, pageBean.getKeyword(), pageBean.getSort(), pageBean.getDirection());
        if (productPageResult != null) {
            return new ResultInfo(200, "select success", productPageResult);
        }
        return new ResultInfo(200, "select fail", null);
    }

    // 根据 productId 查询商品
    @Override
    public ResultInfo selectProductById(Integer productId) {
        Product product = productMapper.selectOne(new QueryWrapper<Product >().eq("product_id", productId));
        if (product != null) {
            return new ResultInfo(200, "select success", product);
        }
        return new ResultInfo(200, "select success", null);
    }

    // 根据种类 categoryId 查询商品
    @Override
    public ResultInfo selectProductByCategoryId(Integer categoryId) {
        List<Product> products = productMapper.selectList(new QueryWrapper<Product>().eq("product_fk_category", categoryId));
        if (products != null) {
            return new ResultInfo(200, "select success", products);
        }
        return new ResultInfo(500, "select fail", null);
    }

    // 修改商品信息
    @Override
    public ResultInfo updateProductInfo(Product product) {

        int i = productMapper.update(product, new UpdateWrapper<Product>().eq("product_name", product.getProductName()));
        if (i > 0) {
            return new ResultInfo(200, "update success", i);
        }
        return new ResultInfo(500, "update fail", i);
    }

    // 根据订单中商品数量，减少商品对应库存

}